package network

import (
	"context"
	"net/http"
	"yoga/identify"
)

func TokenIdentify(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		token := r.Header.Get("token")
		claims, err := identify.IdentifyToken(token)
		if err != nil {
			http.Error(w, "验证失败", http.StatusUnauthorized)
			return
		}
		ctx := context.WithValue(r.Context(), "claims", claims)
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}
